/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view.statementView;
/**
 * 说明：
 * 供应商供货明细表控制界面
 * @Author：黄盛强
 * @Date：2012-03-21
 */
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FileDialog;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.TableRowSorter;

import com.ruipengkj.bean.TSupplier;
import com.ruipengkj.bean.ViewSupplierDetailSummary;
import com.ruipengkj.commons.BuildSpringApplication;
import com.ruipengkj.commons.base.query.PageIndex;
import com.ruipengkj.commons.base.query.PageModel;
import com.ruipengkj.commons.base.query.PageView;
import com.ruipengkj.commons.util.TabelUtil;
import com.ruipengkj.commons.util.Util;
import com.ruipengkj.commons.util.exportExcel.SupplierDetailExportUtil;
import com.ruipengkj.component.JComboBoxPOJO;
import com.ruipengkj.component.JLabelJComboBoxComponent;
import com.ruipengkj.component.RPJButtonBlue;
import com.ruipengkj.component.RPJButtonPrint;
import com.ruipengkj.component.jpanel.RPJPanelStatementTop;
import com.ruipengkj.component.page.PagingComponent;
import com.ruipengkj.model.StatementSupplierDetailModel;
import com.ruipengkj.service.TSupplierService;
import com.ruipengkj.service.ViewSupplierDetailSummaryService;
import com.ruipengkj.view.base.BasePagePanel;

public class StatementSupplierDetail extends BasePagePanel<ViewSupplierDetailSummary> implements Runnable {
	private static final long serialVersionUID = 1L;
	
	private JLabelJComboBoxComponent unitName; //供货商
	
	private JButton selectButton;
	private JButton exportButton;
	
	private JTable table;//供应商供货明细表
	private JScrollPane tableJsp;//供应商供货明细表
	
	private PagingComponent pagingComponent;//翻页组件
	private PageView<ViewSupplierDetailSummary> pageView;//翻页主键
	private StatementSupplierDetailModel tableModel;
	
	private ViewSupplierDetailSummaryService viewSupplierDetailSummaryService;
	private TSupplierService tSupplierService;
	public StatementSupplierDetail() {
		viewSupplierDetailSummaryService=BuildSpringApplication.getInstance().getViewSupplierDetailSummaryServiceImpl();
		tSupplierService=BuildSpringApplication.getInstance().getTSupplierServiceImpl();
		this.setLayout(new BorderLayout());
		this.init();
		this.initListener();
	}
	
	private void init() {
		pageView=new PageView<ViewSupplierDetailSummary>();
		tableModel=new StatementSupplierDetailModel();
		table = uiFactory.createRPJTable(tableModel, null, this, 30, JLabel.CENTER);
		tableJsp = new JScrollPane(table);
		
		this.setOpaque(true);
		this.setBackground(new Color(247, 252, 255));
		table.setOpaque(false);
		tableJsp.setOpaque(false);
		tableJsp.getViewport().setOpaque(false); 
		this.add(uiFactory.createFlowJPanel(), BorderLayout.WEST);
		this.add(uiFactory.createFlowJPanel(), BorderLayout.EAST);
		
		this.add(this.topPanel(),BorderLayout.NORTH);
		this.add(tableJsp,BorderLayout.CENTER);
		this.add(this.tailPanel(),BorderLayout.SOUTH);
		
		// 排序
		TableRowSorter<StatementSupplierDetailModel> tableRowSorter = new TableRowSorter<StatementSupplierDetailModel>(
				tableModel);
		table.setRowSorter(tableRowSorter);
	}
	
	public JPanel topPanel() {
		JPanel panel = new RPJPanelStatementTop();
		unitName = new JLabelJComboBoxComponent("供货商", new String[]{},188);
		unitName.addElement(new JComboBoxPOJO("请选择……"));
		selectButton = new RPJButtonBlue("查询");
		panel.add(unitName);
		panel.add(selectButton);
		return panel;
	}
	
	private JPanel tailPanel() {
		JPanel tail_panel = uiFactory.createBorderJPanel();
		
		JPanel jp = uiFactory.createGridJPanel(1, 2);
		JPanel jp2 = uiFactory.createFlowJPanel(FlowLayout.LEFT);
		exportButton = new RPJButtonPrint("导 出");
		jp2.add(uiFactory.createJLabel("       "));
		jp2.add(exportButton);
		
		pagingComponent=new PagingComponent();
		pagingComponent.setPageDelegale(this);
		Util.setSize(pagingComponent, 80, 24);
		jp.add(jp2);
		jp.add(pagingComponent);
		
		tail_panel.add(jp);
		tail_panel.add(uiFactory.createFlowJPanel(), BorderLayout.NORTH);
		tail_panel.add(uiFactory.createFlowJPanel(), BorderLayout.SOUTH);
		return tail_panel;
	}
	
	private void initListener(){
		selectButton.addActionListener(this);
		exportButton.addActionListener(this);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == selectButton) {
			//查询
			this.termQuery(1);
		}else if(e.getSource()==exportButton){
			//导出
			FileDialog fileDialog = new FileDialog(new JDialog(), "文件导出",FileDialog.SAVE);
			fileDialog.setVisible(true);
			String fileDir = fileDialog.getDirectory();
			String fileName = fileDialog.getFile();
			if (fileDir != null && !fileDir.equals("") && fileName != null&& !fileName.equals("")) {
				if (!fileName.toLowerCase().endsWith(".xls")) {
					if (fileName.lastIndexOf(".") != -1) {
						fileName = fileName.substring(0, fileName.lastIndexOf("."))+ ".xls";
					} else {
						fileName = fileName + ".xls";
					}
				}
				try {
					List<ViewSupplierDetailSummary> list = viewSupplierDetailSummaryService.findExportService(this.queryTerm());
					if (list != null && list.size() != 0) {
						exportButton.setEnabled(false);
						SupplierDetailExportUtil.writeExcel(fileDir + fileName,list);
						JOptionPane.showMessageDialog(null, "导出成功");
					} else {
						JOptionPane.showMessageDialog(null, "数据为空，导出失败");
					}
				} catch (Exception e1) {
					e1.printStackTrace();
					JOptionPane.showMessageDialog(null, "导出失败");
				}
				exportButton.setEnabled(true);
			}
		}
	}
	
	@Override
	protected PageView<ViewSupplierDetailSummary> getPageView() {
		return pageView;
	}

	private ViewSupplierDetailSummary queryTerm(){
		ViewSupplierDetailSummary viewSupplierDetailSummary = new ViewSupplierDetailSummary();
		if(unitName.getBox().getSelectedIndex()!=0){
			JComboBoxPOJO unitNamePo=(JComboBoxPOJO)unitName.getBox().getSelectedItem();//供应商
			viewSupplierDetailSummary.setSupplierName(unitNamePo.getName());
		}
		return viewSupplierDetailSummary;
	}
	
	@Override
	protected void termQuery(final int currentPage) {
		
		new Thread(){
			public void run(){
				tableJsp.getViewport().remove(table);
				tableJsp.getViewport().add(waitComponent);
				try {
					pageView.setCurrentPage(currentPage);
					PageModel<ViewSupplierDetailSummary> pageModel=viewSupplierDetailSummaryService.getScrollService(pageView.getFirstResult(), pageView.getMaxResult(),queryTerm());
					pageView.setPageModel(pageModel);
					pageView.setPageIndex(PageIndex.getPageIndex(pageView.getPageCode(), pageView.getCurrentPage(), pageView.getTotalPage()));
					if(pageModel!=null){
						tableModel.setRowData(pageModel.getResultList());
						SwingUtilities.invokeLater(new Runnable() {
							@Override
							public void run() {
								tableJsp.getViewport().remove(waitComponent);
								tableJsp.getViewport().add(table);
								TabelUtil.setJTabelAutoClumnWidth(table);
								pagingComponent.show();
							}
						});
					}else{
						//没有订单数据
						tableJsp.getViewport().remove(waitComponent);
						tableJsp.getViewport().add(table);
						tableModel.clear();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}.start();
	}

	@Override
	public void run() {
		this.termQuery(1);
		SwingUtilities.invokeLater(querySupping);
	}
	//查询供应商
	private Runnable querySupping=new Runnable() {
		@Override
		public void run() {
			try {
				List<TSupplier> list=tSupplierService.findService(null);
				if(list!=null&&list.size()!=0){
					int length = list.size();
					for(int i = 0; i < length; ++i) {
						TSupplier supplie=list.get(i);
						unitName.addElement(new JComboBoxPOJO(supplie.getSupplierID(),supplie.getName(),supplie));
					}
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	};
	@Override
	public void clear() {
		// TODO Auto-generated method stub
		
	}
}
